<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("cms/core/helper/Abstract.php");

class PHP_CMS_View_Helper_Route extends PHP_CMS_View_Helper_Abstract {

    public function route($routes=array(), $form=null, $rows=null) {

        $route = "";

        if(is_array($routes)) {
            for($i=0; $i<count($routes); $i++) {
                if(is_array($routes[$i])) {
                    $route .= array_key_exists("href", $routes[$i]) ?
                        "<a href=\"".$routes[$i]["href"]."\">".$routes[$i]["name"]."</a>" :
                        $routes[$i]["name"];

                    if($i<count($routes)-1)
                        $route .= " / ";
                } else {
                    $route .= $routes[$i];
                }
            }
        } else {
            $route .= $routes;
        }
        ?>

        <div class="route">
            <div class="left"><?=$route?></div>
            <? if($form !== null && $rows !== null) { ?>
            <div class="right" style="text-align:right"><?=$this->page($form, $rows)?></div>
            <? } ?>
        </div>

        <?
    }

    public function page($form, $rows) {

        if($form->getResults() == "")
            return "1";


        // ----------------------------------------------------- Instance Variables

        // Total number of rows
        $results = $rows;

        // Number of rows per page
        $limit = $form->getResults();

        // Current page (starting with 1)
        $page = $form->getPage();

        // Number of number click links (Must be even number!)
        $links = 10;

        // Half of the links ( for each side )
        $half = round($links / 2);


        // -------------------------------------------------- Calculate result info

        // calculate pages
        $pages = ceil($results / $limit);

        // calculate start
        $start = ($page - 1) * $limit + 1;

        // calculate end
        $end = ($start - 1) + $limit > $results ? $results : ($start - 1) + $limit;


        // ---------------------------------------------------- Calculate link info

        if($pages > $links) {

            // If page > half of #links before the end
            // then start showing more links on the left hand side and keep
            // the total on 10 ($links)
            if($page > $pages - $half) {
                $linkstart = $page - ($links - ($pages - $page)) + 1;
//                echo "<i>1:".$linkstart."</i>";
            // if page > half way -> start taking off links on the left hand side.
            } else if($page > $half + 1) {
                $linkstart = $page - $half;
//                echo "<i>2:".$linkstart."</i>";
            // normal start
            } else {
                $linkstart = 1;
//                echo "<i>3:".$linkstart."</i>";
            }

            // if page + (half of links) > total pages -> end on pages
            if($page + $half > $pages) {
                $linkend = $pages;
            // page is not yet halfway -> end of #links
            } else if($page <= $half) {
                $linkend = $links;
            // else end at page + half of #links
            } else {
                $linkend = $page + $half;
            }
        }

        ?>

        <?=$this->message("label.showing", _Core::BUNDLE)?>: <?=$start?> <?=$this->message("label.showing.to", _Core::BUNDLE)?> <?=$end?> | <?=$this->message("label.showing.of", _Core::BUNDLE)?>: <?=$results?>
        <br />
        <?=$this->message("label.results", _Core::BUNDLE)?>:
        <? if(isset($linkstart)): ?>

            <?=$page > 1 ? "<a href=\"javascript:setPage('".get_class($form)."', 1)\">".$this->message("label.start", _Core::BUNDLE)."</a>" : $this->message("label.start", _Core::BUNDLE)?>
            -
            <?=$page > 1 ? "<a href=\"javascript:setPage('".get_class($form)."',".($page - 1).")\">".$this->message("label.previous", _Core::BUNDLE)."</a>" : $this->message("label.previous", _Core::BUNDLE)?>
            -
            <?for($i=$linkstart; $i<=$linkend; $i++):?>
                <?=$i == $page ? "<b>".$i."</b>" : "<a href=\"javascript:setPage('".get_class($form)."',".$i.")\">".$i."</a>"?></a>
            <?endfor;?>
            -
            <?=$page < $pages ? "<a href=\"javascript:setPage('".get_class($form)."',".($page + 1).")\">".$this->message("label.next", _Core::BUNDLE)."</a>" : $this->message("label.next", _Core::BUNDLE)?>
            -
            <?=$page < $pages ? "<a href=\"javascript:setPage('".get_class($form)."',".$pages.")\">".$this->message("label.end", _Core::BUNDLE)."</a>" : $this->message("label.end", _Core::BUNDLE)?>

        <? else: ?>

            <?for($i=1; $i<=$pages; $i++):?>
                <?=$i == $page ? "<b>".$i."</b>" : "<a href=\"javascript:setPage('".get_class($form)."',".$i.")\">".$i."</a>"?></a>
            <?endfor;?>

        <? endif; ?>
        <br /><br />

    <?

    }

}

?>